package test;

import org.junit.Test;

import sqlengine.Main;

public class DropIndex {
	
	static int score = 0;
	

	
	@Test
	public void dIndex(){
		boolean eCaught = false;
		String query = "create table simple1 (f1 varchar2(10), f2 varchar2(15) primary key);\n";
		String out = null;
		try{
			Main m = new Main();
			out = m.process(query);
			out = m.process("create index in1 on simple1(f1);\n");
			out = m.process("drop index simple1.in1;\n");
			//out = m.process("drop table simple1;\n"); removed
			if(out == null || !out.equals("Index dropped.\n"))
				throw new Exception();
			out = m.process("create index in1 on simple1(f1);\n");
				
		}catch(Exception e){
			eCaught = true;
		}
		
		if(!eCaught && out != null && out.equals("Index created.\n")){
			score+= Breakdown.dIndex;
			SC.score.append("\n\t dropIndex drops index correctly - " + Breakdown.dIndex + "/" + Breakdown.dIndex);
		}else{
			SC.score.append("\n\t dropIndex doesn't drop index correctly  - 0/" + Breakdown.dIndex);
		}	
	}
	

	@Test
	public void dIndexEx(){
		boolean eCaught = false;
		String query1 = "create table simple1 (f1 varchar2(10), f2 varchar2(15) primary key);\n";
		String query2 = "create table simple2 (f1 varchar2(10), f2 varchar2(15) primary key);\n";
		String query3 = "create index in1 on simple1(f1);\n";
		String out = null;
		try{
			Main m = new Main();
			out = m.process("drop index simple1.in1;\n");
			if(out == null || !out.equals("Error\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query1);
			out = m.process("drop index simple1.in1;\n");
			if(out == null || !out.equals("Error\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query1);
			out = m.process(query2);
			out = m.process(query3);
			out = m.process("drop index simple2.in1;\n");
			if(out == null || !out.equals("Error\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query1);
			out = m.process(query2);
			out = m.process(query3);
			out = m.process("drop index simple1.in2;\n");
			if(out == null || !out.equals("Error\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query1);
			out = m.process(query3);
			out = m.process("drop index simple1.in1;\n");
			out = m.process("drop index simple1.in1;\n");
			if(out == null || !out.equals("Error\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query1);
			out = m.process(query3);
			out = m.process("drop index simple1.in1;\n");
			if(out == null || !out.equals("Index dropped.\n"))  //changed from Index created.
				throw new Exception();
			
		}catch(Exception e){
			eCaught = true;
		}
		
		if(!eCaught){  //changed from  out != null && out.equals("Error\n")
			score+= Breakdown.dIndexEx;
			SC.score.append("\n\t dropIndex checks for errors correctly - " + Breakdown.dIndexEx + "/" + Breakdown.dIndexEx);
		}else{
			SC.score.append("\n\t dropIndex doesn't check for errors correctly - 0/" + Breakdown.dIndexEx);
		}	
	}
}
